﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Admin_AddPolicy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }
        }
        if (!IsPostBack)
        {
            ddlCompany.DataSource = this.SelectCompany();
            ddlCompany.DataTextField = "Value";
            ddlCompany.DataValueField = "Key";
            ddlCompany.DataBind();
        }
        Label2.Visible = false;
        Label4.Visible = false;
        Label6.Visible = false;
        Label8.Visible = false;
        Label10.Visible = false;
        Label12.Visible = false;
        Label13.Visible = false;
        Label14.Visible = false;
        success.Visible = false;
    }
    public void AddPolicy(String policyName, string description, decimal amount, decimal emi, int companyId, string medicalId)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("insert into Policies values(@policyName,@description,@amount,@emi,@companyId,@medicalId)", sqlConnect);
        sqlCommand.Parameters.AddWithValue("@policyName", policyName);
        sqlCommand.Parameters.AddWithValue("@description", description);
        sqlCommand.Parameters.AddWithValue("@amount", amount);
        sqlCommand.Parameters.AddWithValue("@emi", emi);
        sqlCommand.Parameters.AddWithValue("@companyId", companyId);
        sqlCommand.Parameters.AddWithValue("@medicalId", medicalId);
        sqlCommand.ExecuteNonQuery();
        sqlConnect.Close();
        success.Visible = true;
        txtMedical.Text = "";
        txtPolicyAmount.Text = "";
        txtPolicyDesc.Text = "";
        txtPolicyEmi.Text = "";
        txtPolicyName.Text = "";
    }

    public Dictionary<int, string> SelectCompany()
    {
        Dictionary<int, string> companyList = new Dictionary<int, string>();
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Select companyId,companyName from CompanyDetails", sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        while (reader.Read())
        {
            companyList.Add(reader.GetInt32(0), reader.GetString(1));
        }
        sqlConnect.Close();

        return companyList;
    }
    protected void btnApply_Click1(object sender, EventArgs e)
    {

        Boolean check = false;
        if (txtPolicyName.Text.Equals(""))
        {
            Label2.Visible = true;
            check = true;
        }

        if (txtPolicyDesc.Text.Equals(""))
        {
            Label4.Visible = true;
            check = true;
        }

        if (txtPolicyAmount.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        else
        {
            try
            {
                decimal amout = Convert.ToDecimal(txtPolicyAmount.Text);
            }
            catch (FormatException ex)
            {
                Label13.Visible = true;
                check = true;
            }

        }

        if (txtPolicyEmi.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        else
        {
            try
            {
                decimal emi = Convert.ToDecimal(txtPolicyEmi.Text);
            }
            catch (FormatException ex)
            {
                Label14.Visible = true;
                check = true;
            }

        }

        if (txtMedical.Text.Equals(""))
        {
            Label12.Visible = true;
            check = true;
        }
        if (!check)
        {
            decimal amount = Convert.ToDecimal(txtPolicyAmount.Text);
            decimal emi = Convert.ToDecimal(txtPolicyEmi.Text);
            this.AddPolicy(txtPolicyName.Text, txtPolicyDesc.Text, amount, emi, Convert.ToInt32(ddlCompany.SelectedValue), txtMedical.Text);
        }
    }
}